package br.rui.iof.visao.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.rui.iof.dominio.TipoLancamentoIOF;
import br.rui.iof.negocio.Fachada;
import br.rui.planilhas.util.Utils;

/**
 * Servlet Class
 *
 * @web.servlet              name="Remover"
 *                           display-name="Name for Remover"
 *                           description="Description for Remover"
 * @web.servlet-mapping      url-pattern="/Remover"
 * @web.servlet-init-param   name="A parameter"
 *                           value="A value"
 */
@SuppressWarnings("serial")
public class RemoverServlet extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException,
		IOException {
		doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException,
		IOException {
		String url = "";
		String acao = req.getParameter("acao");
		
		if(acao != null){
			try {
				if(acao.equalsIgnoreCase("ContasIOF")){
//					url = removerContasIOF(req);
				}
				if(acao.equalsIgnoreCase("LancamentoIOF")){
					url = removerLancamentosIOF(req);
				}
			} catch (Exception e) {
				url = "index.jsp";
				e.printStackTrace();
			}
		}
		req.getRequestDispatcher(url).forward(req,resp);
	}

	private String removerLancamentosIOF(HttpServletRequest req) {
		String url = "";
		
		Date data = Utils.String2Date(req.getParameter("data"));
		TipoLancamentoIOF tipo = TipoLancamentoIOF.setTipo(req.getParameter("tipo"));
		Integer numConta = Integer.parseInt(req.getParameter("numConta"));
		
		Fachada.removerLancamentoIOF(data, tipo, numConta);
		url = "consultarIOF?acao=LancamentosIOF&numConta="+numConta;
		
		return url;
	}

}
